package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.AdSensitive;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author tmh
 * @date 2023/8/27 19:56
 * @description
 */
@Service
@Transactional
@Slf4j
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive>
        implements WmSensitiveService {

    /**
     * 查找所有敏感词
     *
     * @param sensitiveDto
     * @return
     */
    @Override
    public ResponseResult findAll(SensitiveDto sensitiveDto) {
        //1.判断参数是否存在
        if (sensitiveDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.数据库里面查询所有敏感词
        Integer currentPage = sensitiveDto.getPage();
        Integer size = sensitiveDto.getSize();
        Page page = new Page<>(currentPage, size);
        page = page(page, null);
        return ResponseResult.okResult(page.getRecords());
    }

    /**
     * 根据id删除敏感词
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteSensitiveById(Integer id) {
        /**
         * 检查参数是否为空
         */
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 添加敏感词
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult saveSensitive(AdSensitive adSensitive) {
        //1.校验参数是否为空
        if (adSensitive == null || StringUtils.isBlank(adSensitive.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmSensitive wmSensitive = new WmSensitive();
        BeanUtils.copyProperties(adSensitive, wmSensitive);
        wmSensitive.setCreatedTime(new Date());

        log.info(new Date().toString());

        save(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 更新敏感词
     *
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult updateSensitive(AdSensitive adSensitive) {
        //1.判断参数是否为空
        if (adSensitive == null || StringUtils.isBlank(adSensitive.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.执行更新操作
        WmSensitive wmSensitive = new WmSensitive();
        BeanUtils.copyProperties(adSensitive, wmSensitive);
        wmSensitive.setCreatedTime(new Date());
        updateById(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
